<template>
  <!--new-version-->
  <div>
    <h3>{{ question1.text }}</h3>
    <a-radio-group v-model:value="answer1['answer']" @change="handleRadioChange">
      <a-radio
          v-for="option in question1.options"
          :key="option.value"
          :value="option.value"
          @click="handleRadioClick1"
      >
        {{ option.text }}
      </a-radio>
    </a-radio-group>
  </div>
</template>

<script setup>
import {defineProps} from 'vue';
import {useQuestions} from "@/utils/paper.js";
const { getQuestionById,getAnswerById,handleJumpLogic} = useQuestions()
const props = defineProps({
  questionId:{
    type:String
  }
});
const questionId=props.questionId;
const question1=getQuestionById(questionId);
const answer1=getAnswerById(questionId);
const jumpLogic1=question1?.ruleLogic?.jumpLogic;// 该题的跳题逻辑,使用可选链操作符防止null引用错误

const handleRadioChange = () => {
  // 处理跳题逻辑
  jumpLogic1.forEach(rule => {
    const isAnswerMatch = rule.matchValue === answer1['answer'];

    // 条件：根据 isUsing 和匹配结果判断是否启用/禁用跳题逻辑
    if (rule.isUsing && !isAnswerMatch) {
      // 如果当前逻辑已启用但匹配值不一致，禁用跳题
      handleJumpLogic(question1?.questionId, rule.targetQuestionId, true);
      rule.isUsing = false;
    } else if (!rule.isUsing && isAnswerMatch) {
      // 如果当前逻辑未启用且匹配值一致，启用跳题
      handleJumpLogic(question1?.questionId, rule.targetQuestionId, false);
      rule.isUsing = true;
    }
  });
};
// click发生在change改变之前，参数里的e是能拿到待变化的数据的
const handleRadioClick1 = (e) => {
  // 如果点击的选项已选中，取消选择
  if (answer1.answer === e.target.value) {
    // 取消对应的跳题逻辑
    const rule= jumpLogic1.find(logic=>answer1.answer===logic.matchValue);
    if(rule){
      handleJumpLogic(question1?.questionId, rule.targetQuestionId, true);
      rule.isUsing=false;
    }
    answer1.answer = "";  // 清空选中的答案
  }

};
</script>
